<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<link rel="stylesheet" type="text/css" href="${ctx}/css/tipswindown.css"/>
<script type="text/javascript" src="${ctx}/js/tipswindown.js"></script>

<h1 class="page-title"><s:if test="#request.type.toString()=='organization'">部门</s:if><s:elseif test="#request.type.toString()=='promotion'">晋升</s:elseif><s:else>降职</s:else>异动 - ${transaction == null ? "申请" : "修改"}</h1>

<div class="container_12 clearfix leading">

    <div class="side grid_12 leading">
        <form class="form has-validation">

            <div class="clear"></div>
            <div class="clearfix">
                <label class="form-label">选择员工 <em>*</em></label>

                <div class="form-input">
                    <span style="font-size: 15px;font-weight: 600;margin-right: 10px;margin-top:10px;display: none;" class="button" id="employee_name"></span>
                    <a class="button" id="choice_btn" style="margin-top:10px;display: '';" href="javascript:onShowDiv('data_input')"><span style="background:url(${ctx}/images/img/add.png)" class="icon">&nbsp;</span>选择</a>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="old_org" class="form-label">现部门 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="old_org" name="form-name" required="required" placeholder="现部门" readonly="readonly"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="old_duty" class="form-label">现职务 <em>*</em></label>

                <div class="form-input">
                    <input type="text" id="old_duty" name="form-name" required="required" placeholder="现职务" readonly="readonly"/>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="old_grade" class="form-label">现职等职级 <em>*</em></label>

                <div class="form-input">
                    <input type="text" id="old_grade" name="form-name" required="required" placeholder="现职等职级" readonly="readonly"/>
                </div>
            </div>

            <s:if test="#request.type.toString() == 'organization'">
                <div class="clear"></div>
                <div class="clearfix">
                    <label for="new_duty" class="form-label">异动部门 <em>*</em></label>

                    <div class="form-input">
                        <%--<input type="text" id="new_organization" name="form-name" required="required" placeholder="异动部门"/>--%>
                        <select style="width:570px" size="1" name="ClassId" id="new_organization" onchange="onOrgSelChange()"></select>
                    </div>
                </div>
            </s:if>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="new_duty" class="form-label">异动职务 <em>*</em></label>

                <div class="form-input">
                    <%--<input type="text" id="new_duty" name="form-name" required="required" placeholder="异动职务"/>--%>
                    <select style="width:570px" size="1" name="ClassId" id="new_duty" onchange="onDutySelChange()"></select>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">
                <label for="new_grade" class="form-label">异动职等职级 <em>*</em></label>

                <div class="form-input">
                    <%--<input type="text" id="new_grade" name="form-name" required="required" placeholder="异动职等职级"/>--%>
                    <select style="width:570px" size="1" name="ClassId" id="new_grade"></select>
                </div>
            </div>

            <div class="clear"></div>
            <div class="clearfix">

                <label for="t_date" class="form-label">异动时间 <em>*</em></label>
                <div class="form-input">
                    <input type="text" id="t_date" value='<s:date name="#request.transaction.moveDateTiem" format="yyyy-MM-dd"/>' onfocus="WdatePicker({minDate:'%y-%M-%d'})" name="form-name" required="required" placeholder="请输入到期日期" readonly="readonly"/>
                </div>
            </div>

            <div class="form-action clearfix">
                <button class="button" type="button" onclick="checkForm()">${transaction == null ? "申请" : "修改"}</button>
                <button class="button" type="reset">重置</button>
                <s:if test="#request.type.toString() == 'organization'">
                    <a class="button" href="#transaction/organization_main.xhtml"> <span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
                </s:if>
                <s:elseif test="#request.type.toString() == 'promotion'">
                    <a class="button" href="#transaction/promotion_main.xhtml"> <span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
                </s:elseif>
                <s:else>
                    <a class="button" href="#transaction/demotion_main.xhtml"> <span style="background:url(${ctx}/images/img/arrow_undo.png)" class="icon">&nbsp;</span>返回</a>
                </s:else>
            </div>
            <input type="hidden" id="work_id" name="transaction.work.id" value="${transaction.work.id}"/>
            <input type="hidden" name="transaction.id" value="${transaction.id}"/>
            <input type="hidden" name="organizationId" value="${organizationId}"/>
        </form>
    </div>
</div>

<div class="clear"></div>

<script type="text/javascript">
    //显示员工选择框
    function onShowDiv(type) {
        tipsWindown("选择员工", "url:post?${ctx}/employee/staff_list.xhtml?operation=select&organizationId=${organizationId}", "900", "350", "true", "", "true", "text");
    }
    //员工选择框返回事件
    function onSelectEployee(eid, ename) {
        if (eid == undefined || ename == undefined) {
            return;
        }
        $("#employee_name").show();
        $("#employee_name").html(ename);

        //查找该员工相关信息
        $.ajax({
            type:"get",
            url:"/ajax/find_work_info.xhtml?employee_id="+eid,
            dataType:"json",
            success:function (info) {
                if(info != null){
                    //{"work_id":"2","organization_id":"1","organization_name":"技术部","duty_name":"技术部'","grade_name":"技术人员","level_name":"技术人员"}
                    $("#work_id").val(info.work_id);
                    $("#old_org").val(info.organization_name);
                    $("#old_duty").val(info.duty_name);
                    $("#old_grade").val(info.grade_name+"-"+info.level_name);

                    if($("${type}" != "organization")){
                        findDutyByOrgId(info.organization_id);
                    }
                }
            }
        });
    }

    $(function(){
        if("${type}" !=  'organization'){
            if("${transaction != null}" == "true"){
                findDutyByOrgId("${transaction.oldOrganization.id}")
            }
            return;
        }
        $.ajax({
            type:"post",
            url:"/ajax/organization-top.xhtml?parentId=${organizationId}",
            dataType:"json",
            success:function (orginizations) {
                var tempStr = "<option value='-1'>├请选择</option>"
                if(orginizations != null && orginizations.length > 0){
                    for(var i=0; i<orginizations.length; i++){
                        tempStr += "<option value='"+orginizations[i].id+"'>"+orginizations[i].fullName+"</option>"
                    }
                    $("#new_organization").prev("span").html("请选择");
                }
                $("#new_organization").html(tempStr)
                if("${transaction != null}" == "true"){
                    $("#new_organization").prev("span").html("${transaction.newOrganization.fullName}");
                    $("#new_duty").prev("span").html("${transaction.newDuty.dutyName}");
                    $("#new_grade").prev("span").html("${transaction.newGradeLevel.gradeName}${transaction.newGradeLevel.gradeSort}");
                }
            }
        });
    });

    function onOrgSelChange(){
        var orgId = $("#new_organization").val();
        if(orgId > 0){
            findDutyByOrgId(orgId);
        }
        $("#new_duty").prev("span").html("请选择");
        $("#new_grade").prev("span").html("请选择");
    }
    function findDutyByOrgId(orgId){
        $.ajax({
            type:"get",
            url:"/ajax/duty-list.xhtml?organizationId=" + orgId,
            dataType:"json",
            success:function (data) {
                var strHTML = "<option value=\"-1\">请选择</option>";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, duty) {
                        strHTML += "<option value=\"" + duty.id + "\">" + duty.dutyName + "</option>";
                    });
                }
                $("#new_duty").html(strHTML);
            }
        });
    }

    function onDutySelChange(){
        var dutyId = $("#new_duty").val();
        if(dutyId > 0){
            findGradeAndLevelByDutyId(dutyId);
        }
        $("#new_grade").prev("span").html("请选择")
    }
    function findGradeAndLevelByDutyId(dutyId){
        $.ajax({
            type:"get",
            url:"/ajax/grade-and-level.xhtml?dutyId=" + dutyId,
            dataType:"json",
            success:function (data) {
                var strHTML = "<option value=\"-1\">请选择</option>";
                if (data != null && data.length > 0) {
                    $.each(data, function (i, grade) {
                        strHTML += "<option value=\"" + grade.id + "\">" + grade.gradeName + grade.gradeSort + "</option>";
                    });
                }
                $("#new_grade").html(strHTML);
            }
        });
    }

    $(function(){
        if("${transaction != null}" == "true"){
            $("#employee_name").show();
            $("#employee_name").html("${transaction.work.baseId.name}");
            $("#choice_btn").hide();
            $("#old_org").val("${transaction.oldOrganization.fullName}");
            $("#old_duty").val("${transaction.oldDuty.dutyName}");
            $("#old_grade").val("${transaction.oldGradeLevel.gradeName}${transaction.oldGradeLevel.gradeSort}");
        }
    });

    function checkForm(){
        var workId = $("#work_id").val();
        if(workId > 0){
        }else{
            alert("请选择员工");
            return;
        }

        var new_org_id = -1;
        if("${type}" == "organization"){
            new_org_id = $("#new_organization").val();
            if(new_org_id > 0){
            }else{
                alert("请选择异动部门");
                return;
            }
        }

        var new_duty_id = $("#new_duty").val();
        if(new_duty_id > 0){
        }else{
            alert("请选择异动职务");
            return;
        }

        var new_level_id = $("#new_grade").val();
        if(new_level_id > 0){
        }else{
            alert("请选择异动职等职级");
            return;
        }
        var t_date = $("#t_date").val();
        if(checkNull(t_date)){
            alert("请输入异动时间")
            return;
        }

        var data = {
            "transaction.id":"${transaction.id}",
            "transaction.work.id":workId,
            "transaction.newOrganization.id":new_org_id,
            "transaction.newDuty.id":new_duty_id,
            "transaction.newGradeLevel.id":new_level_id,
            "t_date":t_date,
            "organizationId":${organizationId},
            "type":"${type}"
        };
        var url = "${ctx}/transaction/save.xhtml";
        $.ajax(url, {
            type: "POST",
            data: data,
            dataType:"html",
            cache:false,
            success:function(a) {
                return pageDownloaded(a, url)
            },
            complete:function() {
            }
        });
    }
</script>